package cn.uestc.labhms.modules.hms.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * The Email service.
 *
 * @author Mu.tz
 * @since 2020 /1/14 23:22
 */
@Component
public class EmailUtil {
    private static JavaMailSender javaMailSender;
    private static String from;

    @Autowired
    public EmailUtil(JavaMailSender javaMailSender) {
        EmailUtil.javaMailSender = javaMailSender;
    }
    @Value("${spring.mail.username}")
    public void setFrom(String from) {
        EmailUtil.from = from;
    }

    /**
     * 发送纯文本邮件.
     *
     * @param to      目标email 地址
     * @param subject 邮件主题
     * @param text    纯文本内容
     */
    public static void sendMail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        javaMailSender.send(message);
    }
}